﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace RacePlatformer.Manager
{
    public class Animation
    {
        public enum AnimationType { once, contious }
        public AnimationType currentAnimationType;
        public int amountofFrames;
        public int currentFrame;
        public float frameWidth;
        public int startFrame;
        public float frameHeight;
        private TimeSpan animationTimeout;
        public Texture2D texture;
        private TimeSpan lastAnimateTime;
        public bool animationFinished = false;
        /// <summary>
        /// Animation class, making a new animation will allow animation
        /// </summary>
        /// <param name="maingame">The 'MainGame.cs' class</param>
        /// <param name="texturelocation">The location of the texture(relative)</param>
        /// <param name="amountofFrames">The amount of frames that the texture has</param>
        /// <param name="frameWidth">The framewidth for each frame</param>
        /// <param name="frameHeight">The frameheight of each frame</param>
        /// <param name="animationTimeout">The transition time between every frame.</param>
        /// <param name="currentAnimationType">The animation type</param>
        /// <param name="startFrame">optional: the frame at which the animation starts(default = 1)</param>
        public Animation(MainGame maingame, String texturelocation, int amountofFrames, float frameWidth, float frameHeight, TimeSpan animationTimeout, AnimationType currentAnimationType, int startFrame = 0)
        {
            this.texture = maingame.Content.Load<Texture2D>(texturelocation);
            this.amountofFrames = amountofFrames;
            this.frameHeight = frameHeight;
            this.currentFrame = startFrame;
            this.frameWidth = frameWidth;
            this.currentAnimationType = currentAnimationType;
            this.animationTimeout = animationTimeout;
        }
        public void update(GameTime gametime)
        {
            switch (currentAnimationType)
            {
                case AnimationType.contious:
                    //NEED TO FIND WORKAROUND
                    animationFinished = false;
                    if (gametime.TotalGameTime - lastAnimateTime > animationTimeout)
                    {
                        if (currentFrame == amountofFrames)
                        {
                            currentFrame = startFrame;
                            lastAnimateTime = gametime.TotalGameTime;
                        }
                        currentFrame++;
                        lastAnimateTime = gametime.TotalGameTime;
                    }
                    break;
                case AnimationType.once:
                    if (gametime.TotalGameTime - lastAnimateTime > animationTimeout)
                    {
                        if (currentFrame != amountofFrames)
                        {
                            currentFrame++;
                            lastAnimateTime = gametime.TotalGameTime;
                        }
                        else
                        {
                            animationFinished = true;
                        }
                    }
                    break;
            }
        }
        public void resetAnimation()
        {
            currentFrame = 0;
            animationFinished = false;
        }
        public void finishAnimation(GameTime gametime)
        {
            if (gametime.TotalGameTime - lastAnimateTime > animationTimeout)
            {
                if (currentFrame != amountofFrames)
                {
                    currentFrame++;
                    lastAnimateTime = gametime.TotalGameTime;
                }
                else
                {
                    animationFinished = true;
                    currentFrame = startFrame;
                }
            }
        }
    }
}
